package com.sh.ndt.common.dao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;

import com.sh.ndt.exception.DAOException;

@SuppressWarnings("unchecked")
public class ExtractDataRows extends StoredProcedure {

	private static final Logger LOGGER = Logger.getLogger(ExtractDataRows.class);
	private String storedProcedureName = null;

	public ExtractDataRows(DataSource ds, String storedProcedureName, SqlParameter[] sqlParams) throws DAOException {
		super(ds, storedProcedureName);
		super.setParameters(sqlParams);

		this.storedProcedureName = storedProcedureName;
		compile();

	}

	public ExtractDataRows(DataSource dataSource) {
		// TODO Auto-generated constructor stub
	}

	public Map executeStoredProcedure(HashMap inputs) throws DAOException {
		try {
			LOGGER.debug("Calling execute() of ExtractDataRows Inner class");
			constructSPCallSyntax(storedProcedureName, inputs);
			return execute(inputs);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new DAOException(e);
		}
	}

	private void constructSPCallSyntax(String storedProcedureName, HashMap inputParams) {
		Iterator it = inputParams.keySet().iterator();

		StringBuffer query = new StringBuffer();
                query.append("call ");
		query.append(storedProcedureName);
		query.append("(");

		while (it.hasNext()) {
			String key = (String) it.next();
			Object value = inputParams.get(key);

			//query.append("@" + key + "=" + value + ", ");
			query.append("'" + value + "', ");
			// do stuff here
		}
		query.append(")");
		LOGGER.debug(query.toString());
	}
	

}
